package nautilus.moneykeeper.fragment;

import java.io.Serializable;
import java.util.ArrayList;

import nautilus.moneykeeper.MainBaseSlidingActvity;
import nautilus.moneykeeper.R;
import nautilus.moneykeeper.adapter.ListAccountAdapter;
import nautilus.moneykeeper.dao.AccountDAO;
import nautilus.moneykeeper.dto.Account;
import nautilus.moneykeeper.system.Constant;
import nautilus.moneykeeper.utilities.TaskExecuter;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;

public class ListAccountFragment extends Fragment implements OnClickListener{
	public static final String TAG = "ListAccountFragment";
	
	private ImageView mainMenuBtn;
	private ImageButton btnAddNew;
	private ListView lvAccounts;
	
	private ArrayList<Account> tempList;
	private ListAccountAdapter adapter;
	private boolean isButtonClicking;
	
	private Handler _handler = new Handler(new Handler.Callback(){

		@Override
		public boolean handleMessage(Message msg) {
			switch(msg.what){
				case Constant.MSG_LOAD_DATA_DONE:
					bindData();
				break;
				
				case Constant.MSG_LOAD_DATA_ERROR:
				break;
			}
			return false;
		}
	});
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedState){
		View view = inflater.inflate(R.layout.list_accounts, parent, false);
		mainMenuBtn = (ImageView)view.findViewById(R.id.btn_home_menu_slide);
		mainMenuBtn.setOnClickListener(this);
		lvAccounts = (ListView)view.findViewById(R.id.list_account_lvAccount);
		btnAddNew = (ImageButton)view.findViewById(R.id.btn_add_account);
		btnAddNew.setOnClickListener(this);
		
		lvAccounts.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				if(isButtonClicking)
					return;
				isButtonClicking = true;
				AccountDetailFragment addNewFragment = new AccountDetailFragment();
				Bundle args = new Bundle();
				args.putSerializable(Constant.EXTRA_ACCOUNT, (Serializable) adapter.getItem(position));
				addNewFragment.setArguments(args);
				((MainBaseSlidingActvity)getActivity()).setMainFragment(addNewFragment, 
						R.anim.slide_in_from_right, R.anim.slide_out_to_left);
			}
		});
		
		android.util.Log.i("ListAccount", "onResume");
		TaskExecuter.getInstance(getActivity()).execute(new LoadAccountsTask());
		
		return view;		
	}
	
	public void onResume(){
		isButtonClicking = false;
		super.onResume();
	}
	
	class LoadAccountsTask implements Runnable{
		@Override
		public void run(){
			AccountDAO accountDao = new AccountDAO(getActivity());
			tempList = accountDao.getAllAccounts();
			_handler.sendEmptyMessage(Constant.MSG_LOAD_DATA_DONE);
		}
	}
	
	private void onAddNewClicked(){
		((MainBaseSlidingActvity)getActivity()).setMainFragment(UpdateAccountFragment.class, 
				R.anim.slide_in_from_right, R.anim.slide_out_to_left, null);
	}
	
	private void bindData(){
		if(adapter == null){
			adapter = new ListAccountAdapter(getActivity(), tempList);
			lvAccounts.setAdapter(adapter);
			return;
		}
		
		adapter.refresh(tempList);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
			case R.id.btn_home_menu_slide:
				((MainBaseSlidingActvity)getActivity()).toggle();
			break;
			case R.id.btn_add_account:
				onAddNewClicked();
				break;
		}
	}
}
